YAHOO.namespace ("drafTrack"); 

var Dom = YAHOO.util.Dom; var Event = YAHOO.util.Event; var DDM = YAHOO.util.DragDropMgr; 
var ua = YAHOO.env.ua;
var lang = YAHOO.lang;

YAHOO.drafTrack.tableTree = function(id, oConfigs){
		 YAHOO.drafTrack.tableTree.superclass.constructor.call(this,id);
		 if(oConfigs){
		 	if(oConfigs.columns){
		 		this.columns = oConfigs.columns; 
		 	}
		 	if(oConfigs.icons){
		 		this.icons = oConfigs.icons;
		 	}
		 	if(oConfigs.draggable){
		 	this.draggable = true;
		 	}
		 	if(oConfigs.useDragHandle){
		 	this.useDragHandle = true;
		 	}
		 }
		 
		 this.onCellClicked = new YAHOO.util.CustomEvent("onCellClicked");
		 this.onActionClicked = new YAHOO.util.CustomEvent("onActionClicked");         YAHOO.util.Event.addListener(id, "click", this.clickHandler, this, true); 
}
		 
YAHOO.extend(YAHOO.drafTrack.tableTree, YAHOO.widget.TreeView, {

		columns : null, //data for the columns
		icons : null, //Classes for the icons that should be displayed on each column
		draggable : false, //Are the elements inside draggable, need the tabletreeDD is set to true
		useDragHandle : false, //Are we displaying a drag handle on the left of the tree or not
		defaultColumnSize : 120, //column size when not specified in the this.columns data
		defaultExpansion : false, //By default, do we expand the nodes
		oCellEditor : null, //object that contains the editor for updating fields
		onCellClicked : null, //Custom event that is triggered everytime a cell is clicked	
		onActionClicked : null, //Custom event that is triggered everytime an action cell is clicked		 
		odd: true, //Use when updating the nodes	

    				 
		 //override TreeView draw method
		draw:function(){
			this.odd = true;	
			var html = this.root.getHtml();
			var sb=[];
			sb[sb.length] = '<div>' + this.getHeaderHtml() + '</div>'; 
			sb[sb.length] = html;
			var hc=sb.join("");
        	this.getEl().innerHTML = hc;
        	this.firstDraw = false;
        	
        	//If it is draggable tree, we have to wait the display before creating the DD elements
        	if(this.draggable) {
        		//YAHOO.log('init of the Drag and drop behaviour');
        		for (var i = 0; i < this.getRoot().children.length; ++i) this.getRoot().children[i].initDD();
        	}
		},
		
		//generate  header for tabletree
		getHeaderHtml:function(){
	        var sb = [];
	        var col = this.columns;
	        sb[sb.length] = '<table border="0" cellpadding="0" cellspacing="0">';
			sb[sb.length] = '<tr>';
			
			if(this.useDragHandle) sb[sb.length] = '<td class="ygtvdhandlehead"></td>';
			
			
			for(var i=0;i<col.length;i++){
				//We check that the size is defined in the header, else we go with a default value
				var size = (YAHOO.lang.isNumber(col[i].width))?col[i].width:this.defaultColumnSize;
				
				//We save the value in the object to reuse when calculating nodes
				this.columns[i].width = size;
				
				sb[sb.length] = '<td class="ygtvTableHead" width="' + size +'px">' + col[i].header + '</td>';			
			}
			sb[sb.length] = '</tr>';
			sb[sb.length] = '</table>';
			return sb.join("");
		},
		
		//function to automatically load nodes based on json object
		loadNodes: function(nodesData,parentNode) {
//		YAHOO.log(nodesData.length);
		
			if(nodesData.length == 0) return false;

			for(var i=0;i<nodesData.length;i++)
			{
				var tempNode = nodesData[i];
				//YAHOO.log(tempNode);
				//We load the node
				var expanded = (tempNode.expanded)?tempNode.expanded:this.defaultExpansion;
				var addedNode = new YAHOO.drafTrack.TableTreeNode(tempNode,parentNode,expanded);
				//If there are children, we call again the function with children data
				if(tempNode.child) this.loadNodes(nodesData[i].child,addedNode);
				else addedNode.isLeaf = true;
			}
			return true;
			
		},
		/*
		//Add a default node in the tree at the bottom
		addDefaultNode: function() {
			
			//Need to clone the default node before adding it to the tree
			var tempNode = {};
			for(x in this.defaultNode) tempNode[x] = this.defaultNode[x];

			
			var expanded = (tempNode.expanded)?tempNode.expanded:this.defaultExpansion;
			var addedNode = new YAHOO.drafTrack.TableTreeNode(tempNode,this.getRoot(),expanded);
			addedNode.isLeaf = true;
			
			this.draw();
			
		},
		*/
		//Handle clicks on cell by triggering a custom event with the node and the cell clicked
		//you can subscribe to the event with tree.onCellClicked.subscribe(cellClickHandle)
		//returns cellId, node and dataIndex in the event
		clickHandler: function(e) {        	var elTarget = YAHOO.util.Event.getTarget(e);        	var str = /^ygtvcontentel(\d+)(.+)$/i.exec(elTarget.id);        	if(!str) return;
        
        	var node = this.getNodeByIndex(str[1]);

        	var dataIndex = str[2];
        	
        		
        	var columnData = this.getColumnData(dataIndex);
        	
        	//If no data index can be retrieve, it probably means that it is an action
        	if(!columnData)
        	{
        		if(this.isAction(dataIndex))
        		{
        			this.onActionClicked.fire({cellId: elTarget.id, node: node,action: dataIndex});
					this.performAction(dataIndex,node);
        		}
        		else {
        		YAHOO.log("can't decypher the click for dataIndex: " + dataIndex);
        		}
        	}
        	else
        	{
        	//If the cell is editable, we show the editor
        		if(columnData.editor)
        		{
        			if(!this.oCellEditor) this._initCellEditorEl();
        			else if(this.oCellEditor.isActive)
        			{
        				this.cancelCellEditor();
        			}
        	
        			this.displayEditor(node,dataIndex);
        		
//        		YAHOO.log(node);
        		}
        	        		this.onCellClicked.fire({cellId: elTarget.id, node: node,dataIndex: dataIndex});
        	}        	

        },
        
        //return the data of the column based on the column dataIndex
        getColumnData: function(dataIndex)
        {
        	if(!this.columns || this.columns.length == 0) return false;
        	for(var i=0;i<this.columns.length;i++)
        	{
        		if(this.columns[i].dataIndex == dataIndex) return this.columns[i];	
        	}
        	return false;	
        },

        //
        /**		 * Check if the action is a valid action		 *		 * @method isAction
		 * @param actionName {String} value of the actionName to check against tree configuration 		 * @return {Boolean} true if it is a valid action, false if not		 */
        isAction: function(actionName)
        {
        	if(!this.columns || this.columns.length == 0) return false;
        	for(var i=0;i<this.columns.length;i++)
        	{
        		if(this.columns[i].action && this.columns[i].action == actionName) return true;	
        	}
        	return false;	
        },

        //
        /**		 * Perform the action Clicked, only delete for the time being but can be easily extended		 *		 * @method performAction
		 * @param actionName {String} value of the actionName to perform
		 * @param node {node} Node on which the action should be performed		 */
        performAction: function(actionName,node)
        {
        	if(actionName == 'delete')
        	{
        		confirm('are you sure you want to delet this row?');
        		YAHOO.log(node);
        		this.popNode(node);
        		this.draw();
        	}	
        },        
        

		
		//Return a  JSON string that is the image of the tree
		//You can use then loadNodes to load them back
		getTreeJSONData: function() {
			var sb = [];
			for (var i = 0; i < this.getRoot().children.length; ++i) sb[sb.length] = '{' + this.getRoot().children[i].getNodeJSONData() + '}';
			YAHOO.log('[' + sb.join(",") + ']');
			return '[' + sb.join(",") + ']';
		},
		
		
		
		
		//////////////Editor related functions  ///////////
		
		/**		 * Creates HTML markup for Cell Editor.		 *		 * @method _initCellEditorEl		 * @private		 */		_initCellEditorEl : function() {		    // TODO: destroy previous instances		    // Attach Cell Editor container element as first child of body		    var elCellEditor = document.createElement("div");		    elCellEditor.id = this.id + "-celleditor";		    elCellEditor.style.display = "none";
		    elCellEditor.style.position = "absolute";		    elCellEditor.tabIndex = 0;
		    Dom.addClass(elCellEditor, 'cellEditor');
		    
		    elCellEditor.innerHTML='<div id="'+ this.id + '-celleditor-widget"></div><div class="buttons_group"><button onclick="javascript:' + this.getEditorSaveLink() + '">Save</button><button onclick="javascript:' + this.getEditorCancelLink() + '">Cancel</button></div>';
		    
		    			//Dom.addClass(elCellEditor, DT.CLASS_EDITOR);		    var elFirstChild = Dom.getFirstChild(document.body);		    if(elFirstChild) {		        elCellEditor = Dom.insertBefore(elCellEditor, elFirstChild);		    }		    else {		        elCellEditor = document.body.appendChild(elCellEditor);		    }
		    	    	
	    	this.oCellEditor = {};		    // Internal tracker of Cell Editor values		    this.oCellEditor.container = elCellEditor; //store the container element
		    this.oCellEditor.widgetBoxEl = document.getElementById(this.id + '-celleditor-widget');  //Box element that will contain the data		    this.oCellEditor.value = null; //store the value updated, used for saving data when OK is pressed		    this.oCellEditor.isActive = false; //Is the Editor is displayed or not
		    this.oCellEditor.node = null,	//Store the Node that is being updated
		    this.oCellEditor.dataIndex = null //store the data index that is being updated
		   
            		},
		
		
		/**		 * Display the Editor for the cell		 *		 * @method displayEditor		 */
		
		displayEditor: function(node,dataIndex)
		{
			YAHOO.log('display Editor called');
			
			var columnData = this.getColumnData(dataIndex);
			
			//If the editor is only avaialble for a leaf and the node is not a leaf, we don t do anything
			if(columnData.leafDisplayOnly && !node.isLeaf) return;
			
			var oSelf = this;
			// Clear previous Editor            if(this.oCellEditor.isActive) {                this.cancelCellEditor();            }            // Editor not defined            if(!this.oCellEditor) {                return;            }
                
        
            
             // Move Editor            var elContainer = this.oCellEditor.container;
            var cellId = node.contentElId + dataIndex;            var x = Dom.getX(cellId);            var y = Dom.getY(cellId);

			elContainer.style.left = x + "px";            elContainer.style.top = y + "px";
            
            
            //displaying and updating some data
            this.oCellEditor.widgetBoxEl.innerHTML = '';
            this.oCellEditor.value = node.data[dataIndex];
            this.oCellEditor.container.style.display = '';
            
            //Saving also the node and the dataIndex
            this.oCellEditor.node = node;
            this.oCellEditor.dataIndex = dataIndex;
            
            
         	// Handle ESC key
            Event.addListener(elContainer, "keydown", function(e) {            // ESC hides Cell Editor            if(e.keyCode == 27) {              oSelf.cancelCellEditor();            }
            });

            
            // Render Editor markup            var fnEditor;            if(YAHOO.lang.isString(columnData.editor)) {                switch(columnData.editor) {                    /*
                    case "checkbox":                        fnEditor = DT.editCheckbox;                        break;                    case "date":                        fnEditor = DT.editDate;                        break;                    case "radio":                        fnEditor = DT.editRadio;                        break;                    case "textarea":                        fnEditor = DT.editTextarea;                        break;
                    */
                    case "dropdown":                        fnEditor = this.editDropdown;                        break;                        case "textbox":                        fnEditor = this.editTextbox;                        break;
                    default:                        fnEditor = this.editTextbox;                }            }
            else {
            	YAHOO.log('can t display editor, editor type not a string');
            	return;
            }
            
            if(fnEditor) {                // Create DOM input elements                fnEditor.call(this);
            }
            
			
			this.oCellEditor.isActive = true;
			
        	
		},
		
		saveCellEditor: function()
		{

            YAHOO.log('saveCellEditor called');
			if(!this.oCellEditor.isActive) return;
			
//			YAHOO.log(this.oCellEditor.node);
			
			this.oCellEditor.node.data[this.oCellEditor.dataIndex] = this.oCellEditor.value;

			
			this.oCellEditor.node.parent.refresh();

			this.oCellEditor.isActive = false;
			this.oCellEditor.container.style.display = 'none';	
		},
		
		cancelCellEditor: function()
		{
			if(!this.oCellEditor) return;
			if(!this.oCellEditor.isActive) return;
			
			this.oCellEditor.isActive = false;
			this.oCellEditor.container.style.display = 'none';	
		},
		
		
		
		
		/////////////  List of editors ///////////////////////
		
		
		editTextbox: function()
		{	
			
			oSelf = this;
			// Textbox        	var elTextbox;        	// Bug 1802582: SF3/Mac needs a form element wrapping the input        	if(ua.webkit>420) {            	elTextbox = this.oCellEditor.widgetBoxEl.appendChild(document.createElement("form")).appendChild(document.createElement("input"));        	}        	else {            	elTextbox = this.oCellEditor.widgetBoxEl.appendChild(document.createElement("input"));        	}        	elTextbox.type = "text";        	//elTextbox.style.height = "1em"; //(parseInt(elCell.offsetHeight,10)) + "px";        	elTextbox.value = (this.oCellEditor.value)?this.oCellEditor.value:'';        	// Bug: 1802582 Set up a listener on each textbox to track on keypress        	// since SF/OP can't preventDefault on keydown        	Event.addListener(elTextbox, "keypress", function(v){            	// Prevent form submit            	// Save on "enter"            	if((v.keyCode === 13)) {             	   YAHOO.util.Event.preventDefault(v);             	   oSelf.saveCellEditor();            	}        	});        	// Set up a listener on each textbox to track the input value        	Event.addListener(elTextbox, "keyup", function(v){            	// Update the tracker value            	 oSelf.oCellEditor.value = elTextbox.value;        	});        	// Select the text        	elTextbox.focus();        	//	elTextbox.select();
			
		},	

		/**     * Enables SELECT Editor.     *     * @method DataTable.editDropdown     * @static     */    editDropdown : function() {		oSelf = this;		var columnData = this.getColumnData(this.oCellEditor.dataIndex);
		
		        // Textbox        var elDropdown = this.oCellEditor.widgetBoxEl.appendChild(document.createElement("select"));        var dropdownOptions = (columnData.editorOptions && lang.isArray(columnData.editorOptions.dropdownOptions)) ?                columnData.editorOptions.dropdownOptions : [];//        YAHOO.log(columnData);
        for(var j=0; j<dropdownOptions.length; j++) {            var dropdownOption = dropdownOptions[j];
                        var elOption = document.createElement("option");            elOption.value = (lang.isValue(dropdownOption.value)) ?                    dropdownOption.value : dropdownOption;            elOption.innerHTML = (lang.isValue(dropdownOption.text)) ?                    dropdownOption.text : dropdownOption;            elOption = elDropdown.appendChild(elOption);            if(this.oCellEditor.value === elDropdown.options[j].value) {                elDropdown.options[j].selected = true;            }        }        // Set up a listener on each check box to track the input value        Event.addListener(elDropdown, "change",            function(){                oSelf.oCellEditor.value = elDropdown[elDropdown.selectedIndex].value;        });        // Focus the dropdown        this._focusEl(elDropdown);    },
		
		
		
		
		
		
		
		
		
		/** * Sets focus on the given element. * * @method _focusEl * @param el {HTMLElement} Element. * @private */_focusEl : function(el) {    el = el || this._elTbody;    // http://developer.mozilla.org/en/docs/index.php?title=Key-navigable_custom_DHTML_widgets    // The timeout is necessary in both IE and Firefox 1.5, to prevent scripts from doing    // strange unexpected things as the user clicks on buttons and other controls.        // Bug 1921135: Wrap the whole thing in a setTimeout    setTimeout(function() {        setTimeout(function() {            try {                el.focus();            }            catch(e) {            }        },0);    }, 0);},
		
		
		
		
		
		
		getEditorSaveLink: function()
		{
			return 'YAHOO.widget.TreeView.getTree(\'' + this.id +'\').saveCellEditor()';
		},
		
		getEditorCancelLink: function()
		{
			return 'YAHOO.widget.TreeView.getTree(\'' + this.id +'\').cancelCellEditor()';
		}
		
		
		
});



YAHOO.drafTrack.TableTreeNode=function(oData, oParent, expanded){
	YAHOO.drafTrack.TableTreeNode.superclass.constructor.call(this,oData, oParent, expanded,true);
};
	
	
YAHOO.extend(YAHOO.drafTrack.TableTreeNode, YAHOO.widget.HTMLNode, {
		
		odd:false,

    /**
     * Generates the link that will invoke this node's toggle method
     * @method getToggleLink
     * @return {string} the javascript url for toggling this node
     */
    getToggleLink: function() {
        return "YAHOO.widget.TreeView.getNode(\'" + this.tree.id + "\'," + 
            this.index + ").toggle()";
    },
		
		// overrides YAHOO.widget.Node    	getNodeHtml: function() {         var sb = [];		
		sb[sb.length] = '<div id="' + this.getDDId() + '" class="ygtvdd ';
		
		//If the parent is a root, we switch the color for the next node (children node as the same color than the parent)
		if(this.tree.odd && this.parent.isRoot()) {this.tree.odd = false;this.odd=false;}
		else if(!this.tree.odd && this.parent.isRoot()) {this.tree.odd = true;this.odd=true;}
		else this.odd = this.parent.odd;

		//We check what class to add		
		if(this.odd) sb[sb.length] = 'oddLine ';
		else sb[sb.length] = 'evenLine ';

	
		
		sb[sb.length] = '">';
		        sb[sb.length] = '<table border="0" cellpadding="0" cellspacing="0">';        sb[sb.length] = '<tr>';        
        if(this.tree.useDragHandle) sb[sb.length] = '<td id="' + this.getDDHandleId() + '"class="ygtvdhandle"></td>';
        
                for (var i=0;i<this.depth;++i) {            //sb[sb.length] = '<td class="' + this.getDepthStyle(i) + '">&#160;</td>';            sb[sb.length] = '<td class="' + this.getDepthStyle(i) + '"><div class="ygtvspacer"></div></td>';        }
        
        if (this.hasIcon) {            sb[sb.length] = '<td';            sb[sb.length] = ' id="' + this.getToggleElId() + '"';            sb[sb.length] = ' class="' + this.getStyle() + '"';            sb[sb.length] = ' onclick="javascript:' + this.getToggleLink() + '"';            if (this.hasChildren(true)) {                sb[sb.length] = ' onmouseover="this.className=';                sb[sb.length] = 'YAHOO.widget.TreeView.getNode(\'';                sb[sb.length] = this.tree.id + '\',' + this.index +  ').getHoverStyle()"';                sb[sb.length] = ' onmouseout="this.className=';                sb[sb.length] = 'YAHOO.widget.TreeView.getNode(\'';                sb[sb.length] = this.tree.id + '\',' + this.index +  ').getStyle()"';            }            //sb[sb.length] = '>&#160;</td>';            sb[sb.length] = '><div class="ygtvspacer"></div></td>';        }
        
        var col = this.tree.columns;
        for(var i=0;i<col.length;i++)
        {        sb[sb.length] = '<td';                sb[sb.length] = (this.nowrap) ? ' nowrap="nowrap" ' : '';
        //For the first column, we need to remove some width to match the depth
        if(i == 0) {
 			var firstColWidth = col[i].width - 18 * (this.depth + 1);
 			sb[sb.length] = ' width="' + firstColWidth +'"';
 		} else {
 		sb[sb.length] = ' width="' + col[i].width +'"';
 		}

 		//if(this.edited[col[i].dataIndex]) sb[sb.length] = 'edited ';
        
        //if the column is not about data but an action, we display the action details
        if(col[i].action) {
        	sb[sb.length] = ' id="' + this.getCellId(col[i].action) + '"';
        	sb[sb.length] = ' class="' + this.getActionClassName(col[i].action);
        	sb[sb.length] = '">';
        }
        //Else, the column is about data and we display it
        else {
        	        sb[sb.length] = ' id="' + this.getCellId(col[i].dataIndex) + '"';
       	sb[sb.length] = ' class="ygtvCell column_' + col[i].dataIndex + ' ' + this.getColumnClass(col[i].dataIndex) +' ';
 		if(col[i].editor) sb[sb.length] = 'editable ';
        sb[sb.length] = '">';
        //For group, we don t display the data if it should only be displayed for a leaf node (exemple input Type for group)
        if(col[i].leafDisplayOnly && !this.isLeaf) 1;
        else sb[sb.length] = (this.data[col[i].dataIndex])?this.data[col[i].dataIndex]:'&nbsp;';        
        }
        
        sb[sb.length] = '</td>';
        }
                sb[sb.length] = '</tr>';        sb[sb.length] = '</table>';
        sb[sb.length] = '</div>';        return sb.join("");    },
    
    /** 	* Return the correct class for the row, can be customized using icons tree parameter 	* 	* @method getColumnClass 	* @param dataIndex {String} value of the dataIndewx for which we should return the CSS style 	* @return {String} className that should be set for the cell 	*/
    
    getColumnClass: function(dataIndex)
    {
    //	YAHOO.log('getColumnClass for '+ dataIndex);
    	var iconData = this.tree.icons[dataIndex];

    	if(!iconData) return '';

    	if(iconData.mode == 'default') {
    	//	YAHOO.log('default mode');
    		return iconData.iconClass;
    	}
    	
    	if(iconData.mode == 'leafChild') {
    	//	YAHOO.log('leafChild mode');
    		if(this.hasChildren()) return iconData.iconClassChild;
    		else return iconData.iconClassLeaf;
    	}
    	if(iconData.mode == 'dataAvailable') {
    	//	YAHOO.log('dataAvailable mode');
    		if(this.data[dataIndex]) return iconData.iconClassDataAvailable;
    		else return iconData.iconClassDataUnavailable;
    	}
    	if(iconData.mode == 'cellValue') {
    	//YAHOO.log('cellValue mode');
    		var className = iconData.map[this.data[dataIndex]];
    		if(className) return className;
    		else return '';
    		
    	}
    },
    
    getCellId: function(dataIndex)
    {
    	return this.contentElId + dataIndex;
    },
    
    toggle: function()
    {
    	YAHOO.drafTrack.TableTreeNode.superclass.toggle.call(this);
YAHOO.log('toggle called for node '+ this.index);
    	if(this.tree.draggable) this.initDD();
    },
      
  /**  * Return a JSON representation for the node. Usually called by getTreeJSONData  * Can be reinjected in the tree
  *  * @method getNodeJSONData  * @return {String} JSON string for the node and it s children
  */
  
  getNodeJSONData:function() {
        var col = this.tree.columns;
        var nodeData = [];
        var u=0;
        for(var i=0;i<col.length;i++)
        {
        	if(this.data[col[i].dataIndex]) {
        		nodeData[u] = YAHOO.lang.JSON.stringify(col[i].dataIndex) + ":" +YAHOO.lang.JSON.stringify(this.data[col[i].dataIndex]);
        		u++;
        	}
        }
//        YAHOO.log(this.expanded);
        if(this.expanded) 
        {
        	nodeData[u] = '"expanded" : true';
        	u++;
        }
        
        if(this.hasChildren()) {
        	var sb = [];
        	for (var i = 0; i < this.children.length; ++i)
        	{
        		sb[sb.length] = '{' + this.children[i].getNodeJSONData() + '}';
        	} 
        	return nodeData.join(",") + ',"child":[' + sb.join(",") + ']';
        }
        else return nodeData.join(",");
  },
  
  /**  * Return the className for an action. If not specified return ygtv + action name
  *  * @method getActionClassName
  * @param action {String}   * @return {String} claassName for the action
  */
  
  getActionClassName:function(actionName) {
  	//FOr demo purpose, usless as same value than the one returned by default
  	if(actionName == 'delete') return 'ygtvdelete';
  	else return 'ygtv' + actionName;
  }  
    	
});


// Create a class DDNode for some greater purpose 	YAHOO.drafTrack.DDNode = function () {}; 
	YAHOO.drafTrack.DDNode.prototype = {
	
	  /**   * The drag and drop (proxy) to use for this element.   * Not set until initProxies is called   * @property ddProxy   * @type DDProxy   * @default null   */  dd: null,
	
	getDDId : function() {
  	return 'ygtvdd'+this.index;
  	},
  	
	getDDHandleId : function() {
  	return 'ygtvddhandle'+this.index;
  	},  
  		
  	initDD : function() {
    //YAHOO.log('initDD for ' + this.index + ' : '+ this.isDisplayed());
        if (!this.isRoot() && this.isDisplayed('element')) {           if (this.dd == null || this.dd == undefined) {        this.dd = new YAHOO.drafTrack.DDList(this.getDDId(),this);
        
        if(this.tree.useDragHandle) this.dd.setHandleElId(this.getDDHandleId()); 
         
        //YAHOO.log('DD for ' + this.index +' activated');      }
      else {
       //YAHOO.log('removing and recreating DD for ' + this.index +' activated');
       this.dd.unreg();       this.dd = new YAHOO.drafTrack.DDList(this.getDDId(),this); 
       
        if(this.tree.useDragHandle) this.dd.setHandleElId(this.getDDHandleId()); 
      }
         }    

    	for (var i = 0; i < this.children.length; ++i) {
    		//YAHOO.log('calling initDD for '+ this.index + ' child '+this.children[i].index);    		this.children[i].initDD();
    	}
    
    
    if(!this.isRoot() && !this.isDisplayed() && this.dd) {
    //YAHOO.log('unregistering DD for '+ this.index);
    	this.dd.unreg();
    	this.dd = null;
    }  },
      //Check if the node has been rendered or not
    //Usefull to avoid creating drag and drop on non displayed elements
    //Need a mode because if the element is a child, the display none is not stored at the element level 'ygtv' 
    //but at the child level ygtvc
    isDisplayed : function(mode)
    {
    	//YAHOO.log('testing if node '+ this.index +' is displayed');
    	if(this.isRoot()) return true;
    	//First we check that the element exist
    	var exist = document.getElementById(this.getDDId());
    	if(!exist) 
    	{
    		//YAHOO.log('node is not existing');
    		return false;
    	}

    	
    	//second if the element is a parent, we check that the child are displayed
    	if(mode == 'parent' && document.getElementById('ygtvc' +this.index).style.display == 'none' ) 
    	{
    		//YAHOO.log('node is existing but children not displayed');
    		return false;
    	}
    	//fourth we check if the parent is displayed
    	//YAHOO.log('check if parent is displayed');
    	return this.parent.isDisplayed('parent'); 
    },
        
        
    //Check if the node is a descandant of another node
    //Used in order to avoid dropping a parent node on one of its children
    isDescendentOf : function(parentNode) {
    
    //YAHOO.log('test if ' + this.index +' is descendant of ' + parentNode.index);    if (!parentNode || this.isRoot()) return false;    if (this.parent.index == parentNode.index) return true;
    if(this.parent.isRoot()) return false;    return this.parent.isDescendentOf(parentNode);
    },
    
    
    
    
    refresh: function() {
    	this.tree.odd = true;        this.getChildrenEl().innerHTML = this.completeRender();        if (this.hasIcon) {            var el = this.getToggleEl();            if (el) {                el.className = this.getStyle();            }        }
        
        this.initDD();       }
  
};

YAHOO.lang.augment(YAHOO.widget.Node, YAHOO.drafTrack.DDNode,true); 

		  